package com.ruoyi.web.controller.video;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.framework.shiro.service.SysPasswordService;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.video.domain.VideoCompany;
import com.ruoyi.video.service.IVideoCompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户信息Controller
 *
 * @author ruoyi
 * @date 2025-06-11
 */
@Controller
@RequestMapping("/video/videoUser")
@Api("用户")
public class VideoUserController extends BaseController
{
    private String prefix = "video/videoUser";

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IVideoCompanyService companyService;
    @Autowired
    private SysPasswordService passwordService;
    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @RequiresPermissions("video:videoUser:view")
    @GetMapping()
    public String videoUser()
    {
        return prefix + "/videoUser";
    }

    /**
     * 查询用户信息列表
     */
    @RequiresPermissions("video:videoUser:list")
    @PostMapping("/list")
    @ResponseBody
    @ApiOperation("用户列表分页查询")
    public TableDataInfo list(SysUser sysUser)
    {
        startPage();
        List<SysUser> list = sysUserService.selectUserList(sysUser);
        if(CollectionUtils.isNotEmpty(list)){
            list.forEach(e->{
                VideoCompany videoCompany = companyService.selectVideoCompanyById(e.getCompanyId());
                if(videoCompany != null){
                    e.setCompanyName(videoCompany.getCompanyName());
                }
            });
        }
        return getDataTable(list);
    }

    /**
     * 新增用户信息
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        model.addAttribute("list",companyService.selectVideoCompanyList(null));
        return prefix + "/add";
    }

    /**
     * 新增保存用户信息
     */
    @RequiresPermissions("video:videoUser:add")
    @Log(title = "用户信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @ApiOperation("新增用户")
    public AjaxResult addSave(SysUser sysUser)
    {
        if (!sysUserService.checkLoginNameUnique(sysUser))
        {
            return error("新增用户'" + sysUser.getLoginName() + "'失败，登录账号已存在");
        }

        sysUser.setSalt(ShiroUtils.randomSalt());
        sysUser.setPassword(passwordService.encryptPassword(sysUser.getLoginName(), "123456", sysUser.getSalt()));
        int i = sysUserService.insertUser(sysUser);

        SysUserRole userRole = new SysUserRole();
        userRole.setRoleId(100L);
        userRole.setUserId(sysUser.getUserId());
        List<SysUserRole> list = new ArrayList<>();
        list.add(userRole);
        return toAjax(userRoleMapper.batchUserRole(list));
    }

    /**
     * 修改用户信息
     */
    @RequiresPermissions("video:videoUser:edit")
    @GetMapping("/edit/{userId}")
    public String edit(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        SysUser sysUser = sysUserService.selectUserById(userId);
        mmap.put("list",companyService.selectVideoCompanyList(null));
        mmap.put("sysUser", sysUser);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户信息
     */
    @RequiresPermissions("video:videoUser:edit")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    @ApiOperation("修改用户")
    public AjaxResult editSave(SysUser sysUser)
    {
        return toAjax(sysUserService.updateUser(sysUser));
    }

    /**
     * 删除用户信息
     */
    @RequiresPermissions("video:videoUser:remove")
    @Log(title = "用户信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    @ApiOperation("删除用户")
    public AjaxResult remove(String ids)
    {
        return toAjax(sysUserService.deleteUserByIds(ids));
    }
}
